package com.clean.scanlibrary.http

import android.app.Application
import android.text.TextUtils
import android.util.Log
import androidx.lifecycle.AndroidViewModel
import androidx.lifecycle.MutableLiveData
import com.clean.scanlibrary.bean.*
import bb.CD
import bb.CD.Companion.VIEW_FROM_COLOURIZE
import bb.CD.Companion.VIEW_FROM_CURRENCY
import bb.CD.Companion.VIEW_FROM_PIC_STYLE
import bb.CD.Companion.VIEW_FROM_RED_WINE
import bb.CD.Companion.VIEW_FROM_REMOVE_MOIREURL
import com.clean.scanlibrary.camera.utils.Base64Util
import com.clean.scanlibrary.camera.utils.FileUtil
import com.google.gson.Gson
import com.google.gson.reflect.TypeToken
import okhttp3.*
import org.json.JSONArray
import org.json.JSONObject
import java.net.URLEncoder
import java.nio.file.InvalidPathException

class DiscernViewMode(application: Application) :AndroidViewModel(application){

    val getTokenLiveData : MutableLiveData<DiscernTokenBean> = MutableLiveData<DiscernTokenBean>()
    val getPicInfoLiveData : MutableLiveData<ArrayList<DiscernInfoBean>> = MutableLiveData<ArrayList<DiscernInfoBean>>()
    val getWordsLiveData : MutableLiveData<WordInfoBean> = MutableLiveData<WordInfoBean>()
    val getCurrencyLiveData:MutableLiveData<CurrencyInfoBean> = MutableLiveData<CurrencyInfoBean>()
    val getStylePicLiveData:MutableLiveData<CurrencyInfoBean> = MutableLiveData<CurrencyInfoBean>()
    val getRedWineLiveData:MutableLiveData<RedWineBean> = MutableLiveData<RedWineBean>()
    val getCardInfoLiveData:MutableLiveData<CardInfoBean> = MutableLiveData<CardInfoBean>()
    val posHttpError:MutableLiveData<String> = MutableLiveData<String>()

    //-------------------------------美团---------------------------------------------
    val getMtLandscapeLiveData:MutableLiveData<MtLandscapeResponse> = MutableLiveData<MtLandscapeResponse>()

    fun getDiscernToken(appKey:String,appSecret:String){
        val params = "grant_type=client_credentials"+"&client_id=${appKey}"+"&client_secret=${appSecret}"
        HttpManager.getInstance().doGet(HttpApi.discernTokenUrl,params,object :NetCallback{
            override fun onFailure(code: Int, e: Throwable?) {
            }
            override fun onSuccess(info: String?) {
                Log.i("HttpManager==","info==="+info);
                val gson: DiscernTokenBean? = Gson().fromJson(info, DiscernTokenBean::class.java)

                Log.i("HttpManager==","gson==="+gson);

                getTokenLiveData.postValue(gson)


            }
        })
    }



    //上传图像扫描
    fun toUploadPicInfo(token:String,path:String,viewType:Int){
        if(!TextUtils.isEmpty(token) && !TextUtils.isEmpty(path)){
            Log.i("HttpManager","压缩后"+path)
            val imgData: ByteArray = FileUtil.readFileByBytes(path)
            val imgStr: String = Base64Util.encode(imgData)
            val imgParam: String = URLEncoder.encode(imgStr, "UTF-8")
            val mediaType = MediaType.parse("application/x-www-form-urlencoded")
            val body = RequestBody.create(mediaType, "image=${imgParam}&baike_num=1")
            var requestUrl = HttpApi.discernAnimalUrl

            when(viewType){
                CD.VIEW_FROM_BOTANY ->{
                    requestUrl = HttpApi.discernBotanyUrl
                }
                CD.VIEW_FROM_GARDEN ->{
                    requestUrl = HttpApi.discernGardenUrl
                }
                CD.VIEW_FROM_INGREDIENT ->{
                    requestUrl = HttpApi.discernDishUrl
                }
                CD.VIEW_FROM_ANIMAL ->{
                    requestUrl = HttpApi.discernAnimalUrl
                }
                //物体识别
                CD.VIEW_FROM_BODY_SCAN ->{
                    requestUrl = HttpApi.body_scan_url
                }

                CD.VIEW_FROM_CAR_SCAN ->{
                    requestUrl = HttpApi.car_scan_info
                }

                CD.VIEW_FROM_CAR_DETECTION_SCAN ->{
                    requestUrl = HttpApi.car_scan_classify_info
                }
            }
            getDiscernPic("$requestUrl?access_token=$token",body)
        } else {
            posHttpError.postValue("操作频繁，请您稍后。")
        }
    }



    //上传文字识别
    fun toUploadWordInfo(token:String,path:String,viewType:Int){
        if(!TextUtils.isEmpty(token) && !TextUtils.isEmpty(path)){
            Log.i("HttpManager","压缩后"+path)
            val imgData: ByteArray = FileUtil.readFileByBytes(path)
            val imgStr: String = Base64Util.encode(imgData)
            val imgParam: String = URLEncoder.encode(imgStr, "UTF-8")
            val mediaType = MediaType.parse("application/x-www-form-urlencoded")
            val body = RequestBody.create(mediaType, "image=${imgParam}&detect_direction="+true)
            getDiscernWordsPic(HttpApi.discernWordsUrl+"?access_token=$token",body)
        } else {
            posHttpError.postValue("操作频繁，请您稍后。")
        }
    }

    //上传货币识别
    fun toUploadCurrency(token: String,path:String,viewType: Int){
        if(!TextUtils.isEmpty(token) && !TextUtils.isEmpty(path)){
            Log.i("HttpManager","压缩后"+path)
            val imgData: ByteArray = FileUtil.readFileByBytes(path)
            val imgStr: String = Base64Util.encode(imgData)
            val imgParam: String = URLEncoder.encode(imgStr, "UTF-8")
            val mediaType = MediaType.parse("application/x-www-form-urlencoded")
            val body = RequestBody.create(mediaType, "image=${imgParam}")
            getCurrencyInfoPic(HttpApi.discernCurrencyUrl+"?access_token=$token",body,viewType)
        } else {
            posHttpError.postValue("操作频繁，请您稍后。")
        }
    }



    //上传黑白图片
    fun getColourizePic(token: String,path:String,viewType: Int){
        if(!TextUtils.isEmpty(token) && !TextUtils.isEmpty(path)){
            Log.i("HttpManager","压缩后"+path)
            val imgData: ByteArray = FileUtil.readFileByBytes(path)
            val imgStr: String = Base64Util.encode(imgData)
            val imgParam: String = URLEncoder.encode(imgStr, "UTF-8")
            val mediaType = MediaType.parse("application/x-www-form-urlencoded")
            val body = RequestBody.create(mediaType, "image=${imgParam}")
            getCurrencyInfoPic(HttpApi.discerncolourizeUrl+"?access_token=$token",body,viewType)
        } else {
            posHttpError.postValue("操作频繁，请您稍后。")
        }
    }



    //图像特效
    fun getPicspecial(token: String,path:String,viewType: Int,url:String){
        if(!TextUtils.isEmpty(token) && !TextUtils.isEmpty(path)){
            Log.i("HttpManager","压缩后"+path)
            val imgData: ByteArray = FileUtil.readFileByBytes(path)
            val imgStr: String = Base64Util.encode(imgData)
            val imgParam: String = URLEncoder.encode(imgStr, "UTF-8")
            val mediaType = MediaType.parse("application/x-www-form-urlencoded")
            val body = RequestBody.create(mediaType, "image=${imgParam}")
            getCurrencyInfoPic(url+"?access_token=$token",body,viewType)
        } else {
            posHttpError.postValue("操作频繁，请您稍后。")
        }
    }



//    //人物漫画
//    fun getPeopleHeader(token: String,path:String,viewType: Int){
//        if(!TextUtils.isEmpty(token) && !TextUtils.isEmpty(path)){
//            Log.i("HttpManager","压缩后"+path)
//            val imgData: ByteArray = FileUtil.readFileByBytes(path)
//            val imgStr: String = Base64Util.encode(imgData)
//            val imgParam: String = URLEncoder.encode(imgStr, "UTF-8")
//            val mediaType = MediaType.parse("application/x-www-form-urlencoded")
//            val body = RequestBody.create(mediaType, "image=${imgParam}")
//            getCurrencyInfoPic(HttpApi.stylePeopleUrl+"?access_token=$token",body,viewType)
//        } else {
//            posHttpError.postValue("操作频繁，请您稍后。")
//        }
//    }



    //红酒识别
    fun getRedWine(token: String,path:String,viewType: Int){
        if(!TextUtils.isEmpty(token) && !TextUtils.isEmpty(path)){
            Log.i("HttpManager","压缩后"+path)
            val imgData: ByteArray = FileUtil.readFileByBytes(path)
            val imgStr: String = Base64Util.encode(imgData)
            val imgParam: String = URLEncoder.encode(imgStr, "UTF-8")
            val mediaType = MediaType.parse("application/x-www-form-urlencoded")
            val body = RequestBody.create(mediaType, "image=${imgParam}")
            getCurrencyInfoPic(HttpApi.redWineUrl+"?access_token=$token",body,viewType)
        } else {
            posHttpError.postValue("操作频繁，请您稍后。")
        }
    }




    //上传风格图片
    fun getStylesTransPic(token: String,path:String,viewType: Int,style:String){
        if(!TextUtils.isEmpty(token) && !TextUtils.isEmpty(path)){
            Log.i("HttpManager","压缩后"+path)
            val imgData: ByteArray = FileUtil.readFileByBytes(path)
            val imgStr: String = Base64Util.encode(imgData)
            val imgParam: String = URLEncoder.encode(imgStr, "UTF-8")
            val mediaType = MediaType.parse("application/x-www-form-urlencoded")
            val body = RequestBody.create(mediaType, "image=${imgParam}&option="+style)
            getCurrencyInfoPic(HttpApi.styleTransUrl+"?access_token=$token",body,viewType)
        } else {
            posHttpError.postValue("操作频繁，请您稍后。")
        }
    }



    //识别通行证
    fun getDiscernCardPic(url:String,params: RequestBody){
        HttpManager.getInstance().doPostForDiscern(url,params,object :NetCallback{
            override fun onFailure(code: Int, e: Throwable?) {
            }
            override fun onSuccess(info: String?) {
                try {

                    val json = JSONObject(info)
                    val string = json.getString("words_result")

                    val gson = Gson().fromJson(string,CardInfoBean::class.java)
                    getCardInfoLiveData.postValue(gson)

                }catch (e:java.lang.Exception){
                    posHttpError.postValue("今日次数已经用完")
                }

            }
        })
    }


    //上传图像识别
    fun getDiscernPic(url:String,params: RequestBody){
        HttpManager.getInstance().doPostForDiscern(url,params,object :NetCallback{
            override fun onFailure(code: Int, e: Throwable?) {
            }
            override fun onSuccess(info: String?) {
                try {

                    Log.i("ddd==","url===="+url)


                    val json = JSONObject(info)
                    val string = json.getString("result")
                    val gson: ArrayList<DiscernInfoBean>? = Gson().fromJson(string,object : TypeToken<ArrayList<DiscernInfoBean>>(){}.type)
                    getPicInfoLiveData.postValue(gson)
                }catch (e:java.lang.Exception){
                    posHttpError.postValue("今日次数已经用完")
                }

            }
        })
    }

    //上传公式识别
    fun toUploadFormulaInfo(token:String,path:String,viewType:Int){
        if(!TextUtils.isEmpty(token) && !TextUtils.isEmpty(path)){
            Log.i("HttpManager","压缩后"+path)
            val imgData: ByteArray = FileUtil.readFileByBytes(path)
            val imgStr: String = Base64Util.encode(imgData)
            val imgParam: String = URLEncoder.encode(imgStr, "UTF-8")
            val mediaType = MediaType.parse("application/x-www-form-urlencoded")
            val body = RequestBody.create(mediaType, "image=${imgParam}&paragraph="+true+"&detect_direction="+true )
            getDiscernWordsPic(HttpApi.formulaWordsUrl+"?access_token=$token",body)
        } else {
            posHttpError.postValue("操作频繁，请您稍后。")
        }
    }

    fun getDiscernWordsPic(url:String,params: RequestBody){
        HttpManager.getInstance().doPostForDiscern(url,params,object :NetCallback{
            override fun onFailure(code: Int, e: Throwable?) {

            }
            override fun onSuccess(info: String?) {
                val gson:WordInfoBean = Gson().fromJson(info,WordInfoBean::class.java)
                getWordsLiveData.postValue(gson)
            }
        })
    }


    fun getCurrencyInfoPic(url:String,params: RequestBody,viewType: Int){
        HttpManager.getInstance().doPostForDiscern(url,params,object : NetCallback{
            override fun onFailure(code: Int, e: Throwable?) {
            }

            override fun onSuccess(json: String?) {
                val result = JSONObject(json)

                when(viewType){
                    //货币识别
                    VIEW_FROM_CURRENCY->{
                        try {
                            val gson = Gson().fromJson(result.getString("result"),CurrencyInfoBean::class.java)
                            getCurrencyLiveData.postValue(gson)
                        }catch (e:Exception){
                            posHttpError.postValue("识别失败")
                        }

                    }

                    //黑白图上色
                    VIEW_FROM_REMOVE_MOIREURL ->{
                        val gson = Gson().fromJson(json,CurrencyInfoBean::class.java)
                        val bean = CurrencyInfoBean("",0,"","","","","",gson.image_processed)
                        getCurrencyLiveData.postValue(bean)
                    }
                    CD.VIEW_FROM_CONTRAST_ENHANCE,
                    CD.VIEW_FROM_DEHAZE,
                    CD.VIEW_FROM_PIC_PEOPLE,
                    VIEW_FROM_COLOURIZE->{
                        try {
                            val gson = Gson().fromJson(json,CurrencyInfoBean::class.java)
                            getCurrencyLiveData.postValue(gson)
                        }catch (e:Exception){
                            posHttpError.postValue("识别失败")
                        }

                    }
                    VIEW_FROM_PIC_STYLE->{

                        try {
                            val gson = Gson().fromJson(json,CurrencyInfoBean::class.java)
                            getStylePicLiveData.postValue(gson)
                        }catch (e:Exception){
                            posHttpError.postValue("识别失败")
                        }

                    }

                    //红酒识别
                    VIEW_FROM_RED_WINE->{
                        try {
                            val gson = Gson().fromJson(result.getString("result"), RedWineBean::class.java)
                            getRedWineLiveData.postValue(gson)
                        }catch (e:Exception){
                            posHttpError.postValue("识别失败")
                        }

                    }
                }


            }
        })
    }

    //---------------------------美团相关-----------------------------
    //-----------------风景去噪-----------------------------
    fun toMeiTuanloadLandScape(path:String){
        if(!TextUtils.isEmpty(path)){
            Log.i("HttpManager","压缩后"+path)
            val imgData: ByteArray = FileUtil.readFileByBytes(path)
            val imgStr: String = Base64Util.encode(imgData)
            // val imgParam: String = URLEncoder.encode(imgStr, "UTF-8")
//            val params = mutableMapOf<String,Any>()
//            val tempOne = mutableMapOf<String,String>()
//            tempOne.put("rsp_media_type","base64")
//            tempOne.put("version","1.0")
//            params.put("parameter",tempOne)
//            var list = mutableListOf<MtLandscape>()
//            var profiles = MtLandscape.MediaProfiles("png")
//            var landscape = MtLandscape(imgParam,profiles)
//            list.add(landscape)
//            params.put("media_info_list",list)
//            var paramsJson = JSONObject(params as Map<*, *>?)
//            Log.d("HttpManager","paramsJson>>"+paramsJson.toString())

            var jsonObject = JSONObject()
            var parameter = JSONObject()
            parameter.put("rsp_media_type","base64")
            parameter.put("version","1.0")

            var mediaObject = JSONObject()
            mediaObject.put("media_data",imgStr)
            var mediaProfiles = JSONObject()
            mediaProfiles.put("media_data_type","jpg")
            mediaObject.put("media_profiles",mediaProfiles)

            var mediaArray = JSONArray()
            mediaArray.put(mediaObject)

            jsonObject.put("parameter",parameter)
            jsonObject.put("media_info_list",mediaArray)

            Log.d("HttpManager", " 参数》》$jsonObject")
            val mediaType = MediaType.parse("application/json; charset=utf-8")
            val body = RequestBody.create(mediaType, jsonObject.toString())
            getMeiTuanLandscape(HttpApi.landscapeReduceUrl,body)
        } else {
            posHttpError.postValue("操作频繁，请您稍后。")
        }
    }
    fun getMeiTuanLandscape(url:String,params: RequestBody){
        HttpManager.getInstance().doPostForMeiTuan(url,params,object :NetCallback{
            override fun onFailure(code: Int, e: Throwable?) {

            }
            override fun onSuccess(info: String?) {
                Log.d("HttpManager","动漫化身返回》》"+info.toString())
                try {
                    val gson: MtLandscapeResponse =
                        Gson().fromJson(info, MtLandscapeResponse::class.java)
                    getMtLandscapeLiveData.postValue(gson)
                } catch (e:Exception){
                    posHttpError.postValue("人脸缺失")
                }
            }
        })
    }

    //-------------------动漫化身----------------
    fun toMeiTuanAnimeAvatar(path:String){
        if(!TextUtils.isEmpty(path)){
            Log.i("HttpManager","压缩后"+path)
            val imgData: ByteArray = FileUtil.readFileByBytes(path)
            val imgStr: String = Base64Util.encode(imgData)

            var jsonObject = JSONObject()
            var parameter = JSONObject()
            parameter.put("rsp_media_type","jpg")
            parameter.put("version","1.0.0")

            var mediaObject = JSONObject()
            mediaObject.put("media_data",imgStr)
            var mediaProfiles = JSONObject()
            mediaProfiles.put("media_data_type","jpg")
            mediaObject.put("media_profiles",mediaProfiles)

            var mediaArray = JSONArray()
            mediaArray.put(mediaObject)

            jsonObject.put("parameter",parameter)
            jsonObject.put("media_info_list",mediaArray)

            Log.d("HttpManager", " 参数》》$jsonObject")
            val mediaType = MediaType.parse("application/json; charset=utf-8")
            val body = RequestBody.create(mediaType, jsonObject.toString())
            getMeiTuanLandscape(HttpApi.animeAvatarUrl,body)
        } else {
            posHttpError.postValue("操作频繁，请您稍后。")
        }
    }

    //-----------------美颜技术--------------------------
    fun toMeiTuanBeautyTechnology(path:String){
        if(!TextUtils.isEmpty(path)){
            Log.i("HttpManager","压缩后"+path)
            val imgData: ByteArray = FileUtil.readFileByBytes(path)
            val imgStr: String = Base64Util.encode(imgData)

            var jsonObject = JSONObject()
            var parameter = JSONObject()
            parameter.put("beauty_alpha","90")


            var mediaObject = JSONObject()
            mediaObject.put("media_data",imgStr)
            var mediaProfiles = JSONObject()
            mediaProfiles.put("media_data_type","jpg")
            mediaObject.put("media_profiles",mediaProfiles)

            var mediaArray = JSONArray()
            mediaArray.put(mediaObject)

            jsonObject.put("parameter",parameter)
            jsonObject.put("media_info_list",mediaArray)

            Log.d("HttpManager", " 参数》》$jsonObject")
            val mediaType = MediaType.parse("application/json; charset=utf-8")
            val body = RequestBody.create(mediaType, jsonObject.toString())
            getMeiTuanLandscape(HttpApi.beautyTechnologyUrl,body)
        } else {
            posHttpError.postValue("操作频繁，请您稍后。")
        }
    }

    //-----------------像素风----------------------------

    fun toMeiTuanpPixelStyle(path:String){
        if(!TextUtils.isEmpty(path)){
            Log.i("HttpManager","压缩后"+path)
            val imgData: ByteArray = FileUtil.readFileByBytes(path)
            val imgStr: String = Base64Util.encode(imgData)

            var jsonObject = JSONObject()
            var parameter = JSONObject()
            parameter.put("nImgStyleIdx","30104")
            parameter.put("rsp_media_type","jpg")

            var mediaObject = JSONObject()
            mediaObject.put("media_data",imgStr)
            var mediaProfiles = JSONObject()
            mediaProfiles.put("media_data_type","jpg")
            mediaObject.put("media_profiles",mediaProfiles)

            var mediaArray = JSONArray()
            mediaArray.put(mediaObject)

            jsonObject.put("parameter",parameter)
            jsonObject.put("media_info_list",mediaArray)

            Log.d("HttpManager", " 参数》》$jsonObject")
            val mediaType = MediaType.parse("application/json; charset=utf-8")
            val body = RequestBody.create(mediaType, jsonObject.toString())
            getMeiTuanLandscape(HttpApi.mtPixelStyleUrl,body)
        } else {
            posHttpError.postValue("操作频繁，请您稍后。")
        }
    }

    //----------------百变滤镜----鉴权问题调用失败----------------
    fun toMeiTuanpVersatileFilter(path:String){
        if(!TextUtils.isEmpty(path)){
            Log.i("HttpManager","压缩后"+path)
            val imgData: ByteArray = FileUtil.readFileByBytes(path)
            val imgStr: String = Base64Util.encode(imgData)

            var jsonObject = JSONObject()
            var parameter = JSONObject()
            parameter.put("filterType","Fa0145JwLfwjjbch")
            parameter.put("filterAlpha","70")//滤镜透明度，默认值 70
            parameter.put("beautyAlpha","70")//美颜透明度，默认值 0
            parameter.put("virtualType","20")//是否开启虚化效果，默认值为 0，范围为 0-100，数值越大表示虚化程度越深

            parameter.put("rsp_media_type","base64")

            var mediaObject = JSONObject()
            mediaObject.put("media_data",imgStr)
            var mediaProfiles = JSONObject()
            mediaProfiles.put("media_data_type","jpg")
            mediaObject.put("media_profiles",mediaProfiles)

            var mediaArray = JSONArray()
            mediaArray.put(mediaObject)

            jsonObject.put("parameter",parameter)
            jsonObject.put("media_info_list",mediaArray)

            Log.d("HttpManager", " 参数》》$jsonObject")
            val mediaType = MediaType.parse("application/json; charset=utf-8")
            val body = RequestBody.create(mediaType, jsonObject.toString())
            getMeiTuanLandscape(HttpApi.mtVersatileFilterUrl,body)
        } else {
            posHttpError.postValue("操作频繁，请您稍后。")
        }
    }

    //----------------神奇变脸----------------------
    fun toMeiTuanpFaceChanging(path:String){
        if(!TextUtils.isEmpty(path)){
            Log.i("HttpManager","压缩后"+path)
            val imgData: ByteArray = FileUtil.readFileByBytes(path)
            val imgStr: String = Base64Util.encode(imgData)

            var jsonObject = JSONObject()
            var parameter = JSONObject()
            parameter.put("outputType","2")//输出图内容，取值范围为 1 、2 、3 分别代表不同的胡子效果
            parameter.put("rsp_media_type","base64")

            var mediaObject = JSONObject()
            mediaObject.put("media_data",imgStr)
            var mediaProfiles = JSONObject()
            mediaProfiles.put("media_data_type","jpg")
            mediaObject.put("media_profiles",mediaProfiles)

            var mediaArray = JSONArray()
            mediaArray.put(mediaObject)

            jsonObject.put("parameter",parameter)
            jsonObject.put("media_info_list",mediaArray)

            Log.d("HttpManager", " 参数》》$jsonObject")
            val mediaType = MediaType.parse("application/json; charset=utf-8")
            val body = RequestBody.create(mediaType, jsonObject.toString())
            getMeiTuanLandscape(HttpApi.mtFaceChangeUrl,body)
        } else {
            posHttpError.postValue("操作频繁，请您稍后。")
        }
    }

    //----------------食物分割--------------------
    fun toMeiTuanFoodSeparation(path: String){
        if(!TextUtils.isEmpty(path)){
            Log.i("HttpManager","压缩后"+path)
            val imgData: ByteArray = FileUtil.readFileByBytes(path)
            val imgStr: String = Base64Util.encode(imgData)

            var jsonObject = JSONObject()
            var parameter = JSONObject()
            parameter.put("rsp_media_type","base64")

            var mediaObject = JSONObject()
            mediaObject.put("media_data",imgStr)
            var mediaProfiles = JSONObject()
            mediaProfiles.put("media_data_type","jpg")
            mediaObject.put("media_profiles",mediaProfiles)

            var mediaArray = JSONArray()
            mediaArray.put(mediaObject)

            jsonObject.put("parameter",parameter)
            jsonObject.put("media_info_list",mediaArray)

            Log.d("HttpManager", " 参数》》$jsonObject")
            val mediaType = MediaType.parse("application/json; charset=utf-8")
            val body = RequestBody.create(mediaType, jsonObject.toString())
            getMeiTuanLandscape(HttpApi.mtFoodSeparationUrl,body)
        } else {
            posHttpError.postValue("操作频繁，请您稍后。")
        }
    }

    //----------------图像画质修复------------------
    fun toMeiTuanImageQualityRestore(path: String){
        if(!TextUtils.isEmpty(path)){
            Log.i("HttpManager","压缩后"+path)
            val imgData: ByteArray = FileUtil.readFileByBytes(path)
            val imgStr: String = Base64Util.encode(imgData)

            var jsonObject = JSONObject()
            var parameter = JSONObject()
            parameter.put("rsp_media_type","base64")

            var mediaObject = JSONObject()
            mediaObject.put("media_data",imgStr)
            var mediaProfiles = JSONObject()
            mediaProfiles.put("media_data_type","jpg")
            mediaObject.put("media_profiles",mediaProfiles)

            var mediaArray = JSONArray()
            mediaArray.put(mediaObject)

            jsonObject.put("parameter",parameter)
            jsonObject.put("media_info_list",mediaArray)

            Log.d("HttpManager", " 参数》》$jsonObject")
            val mediaType = MediaType.parse("application/json; charset=utf-8")
            val body = RequestBody.create(mediaType, jsonObject.toString())
            getMeiTuanLandscape(HttpApi.mtImageQualityRestoreUrl,body)
        } else {
            posHttpError.postValue("操作频繁，请您稍后。")
        }
    }

    //----------------证件照生成------------------
    fun toMeiTuanPhotoMake(path: String){
        if(!TextUtils.isEmpty(path)){
            Log.i("HttpManager","压缩后"+path)
            val imgData: ByteArray = FileUtil.readFileByBytes(path)
            val imgStr: String = Base64Util.encode(imgData)

            var jsonObject = JSONObject()
            var parameter = JSONObject()
            parameter.put("rsp_media_type","base64")
            parameter.put("outputType","3")//输出的图片类型，默认值 3，可选值为 0、1、3、1000：0 表示调用检测规则，是否符合检测规则；1 表示返回美颜的图片；3 表示返回的是带 mask 的图；1000 表示返回裁剪后的图片（注：outputType=1000 的输入图片必须为带 mask 的图片，即 outputType=3 返回的图片）

            var mediaObject = JSONObject()
            mediaObject.put("media_data",imgStr)
            var mediaProfiles = JSONObject()
            mediaProfiles.put("media_data_type","jpg")
            mediaObject.put("media_profiles",mediaProfiles)

            var mediaArray = JSONArray()
            mediaArray.put(mediaObject)

            jsonObject.put("parameter",parameter)
            jsonObject.put("media_info_list",mediaArray)

            Log.d("HttpManager", " 参数》》$jsonObject")
            val mediaType = MediaType.parse("application/json; charset=utf-8")
            val body = RequestBody.create(mediaType, jsonObject.toString())
            getMeiTuanLandscape(HttpApi.mtPhotoMakeUrl,body)
        } else {
            posHttpError.postValue("操作频繁，请您稍后。")
        }
    }

    //----------------图像风格化--------------
    fun toMeiTuanImageStyle(path: String){
        if(!TextUtils.isEmpty(path)){
            Log.i("HttpManager","压缩后"+path)
            val imgData: ByteArray = FileUtil.readFileByBytes(path)
            val imgStr: String = Base64Util.encode(imgData)

            var jsonObject = JSONObject()
            var parameter = JSONObject()
            parameter.put("rsp_media_type","base64")
            parameter.put("styleId","5")//风格 Id，可用值 1～11，默认值 1

            var mediaObject = JSONObject()
            mediaObject.put("media_data",imgStr)
            var mediaProfiles = JSONObject()
            mediaProfiles.put("media_data_type","jpg")
            mediaObject.put("media_profiles",mediaProfiles)

            var mediaArray = JSONArray()
            mediaArray.put(mediaObject)

            jsonObject.put("parameter",parameter)
            jsonObject.put("media_info_list",mediaArray)

            Log.d("HttpManager", " 参数》》$jsonObject")
            val mediaType = MediaType.parse("application/json; charset=utf-8")
            val body = RequestBody.create(mediaType, jsonObject.toString())
            getMeiTuanLandscape(HttpApi.mtImageStyleUrl,body)
        } else {
            posHttpError.postValue("操作频繁，请您稍后。")
        }
    }

    //----------------线稿--------------------

    fun toMeiTuanXianGaoStyle(path:String){
        if(!TextUtils.isEmpty(path)){
            Log.i("HttpManager","压缩后"+path)
            val imgData: ByteArray = FileUtil.readFileByBytes(path)
            val imgStr: String = Base64Util.encode(imgData)

            var jsonObject = JSONObject()
            var parameter = JSONObject()
            parameter.put("rsp_media_type","jpg")

            var mediaObject = JSONObject()
            mediaObject.put("media_data",imgStr)
            var mediaProfiles = JSONObject()
            mediaProfiles.put("media_data_type","jpg")
            mediaObject.put("media_profiles",mediaProfiles)

            var mediaArray = JSONArray()
            mediaArray.put(mediaObject)

            jsonObject.put("parameter",parameter)
            jsonObject.put("media_info_list",mediaArray)

            Log.d("HttpManager", " 参数》》$jsonObject")
            val mediaType = MediaType.parse("application/json; charset=utf-8")
            val body = RequestBody.create(mediaType, jsonObject.toString())
            getMeiTuanLandscape(HttpApi.mtXiangaoUrl,body)
        } else {
            posHttpError.postValue("操作频繁，请您稍后。")
        }
    }
}